package com.zqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqc.entity.OrderDetail;
import com.zqc.entity.Orders;
import com.zqc.entity.ShoppingCart;
import com.zqc.mapper.OrdersMapper;
import com.zqc.service.OrderDetailService;
import com.zqc.service.OrdersService;
import com.zqc.service.ShoppingCartService;
import com.zqc.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    public OrderDetailService orderDetailService;

    @Autowired
    public ShoppingCartService shoppingCartService;

    /**
     * 查询订单信息，加入购物车
     *
     * @param orders
     */
    @Override
    public void again(Orders orders) {
        // 根据订单id查询订单详情
        List<OrderDetail> orderDetails = orderDetailService.list(new LambdaQueryWrapper<OrderDetail>()
                .eq(OrderDetail::getOrderId, orders.getId()));

        // 将订单详情信息设置到购物车
        List<ShoppingCart> shoppingCarts = orderDetails.stream().map(item -> {
            // 创建购物车对象
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(item.getName());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(item.getDishId());
            shoppingCart.setSetmealId(item.getSetmealId());
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            return shoppingCart;
        }).collect(Collectors.toList());
        // 添加到购物车
        shoppingCartService.saveBatch(shoppingCarts);
    }
}
